﻿#pragma once
#include <assert.h>
#include <iostream>
using namespace std;

template<class K, class V>
struct AVLTreeNode
{
	pair<K, V> _kv;//k:v结构
	AVLTreeNode<K, V>* _left;
	AVLTreeNode<K, V>* _right;
	AVLTreeNode<K, V>* _perent;
	int _bf;//平衡因子

	AVLTreeNode(const pair<K, V>& kv)//构造
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _perent(nullptr)
		, _bf(0)
	{}
};

template<class K, class V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;//重命名
public:
	//插入
	bool Insert(const pair<K, V>& kv)
	{
		//树为空
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}

		//树不为空
		Node* cur = _root;
		Node* perent = nullptr;
		while (cur)
		{
			if (kv.first > cur->_kv.first)
			{
				perent = cur;
				cur = cur->_right;
			}
			else if (kv.first < cur->_kv.first)
			{
				perent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}

		//插入
		cur = new Node(kv);
		if (kv.first > perent->_kv.first)
		{
			perent->_right = cur;
		}
		else
		{
			perent->_left = cur;
		}
		cur->_perent = perent;

		//更新平衡因子
		while (perent)
		{
			if (cur == perent->_left)//插入节点位于左边
			{
				--perent->_bf;//平衡因子--
			}
			else//插入节点位于右边
			{
				++perent->_bf;//平衡因子++
			}

			if (perent->_bf == 0)//平衡因子等于0
			{
				break;//直接结束
			}
			else if (perent->_bf == -1 || perent->_bf == 1)//平衡因子需要继续向上更新
			{
				cur = perent;//更新cur为父节点
				perent = perent->_perent;//更新父节点为父父节点
			}
			else if (perent->_bf == -2 || perent->_bf == 2)//不满足AVL树，需要旋转处理
			{
				//旋转
				if (perent->_bf == -2 && cur->_bf == -1)//左边高
				{
					RotateR(perent);//右单旋
				}
				else if (perent->_bf == 2 && cur->_bf == 1)//右边高
				{
					RotateL(perent);//左单旋
				}
				else if (perent->_bf == -2 && cur->_bf == 1)//左的中间高
				{
					RotateLR(perent);//左右双旋
				}
				else if (perent->_bf == 2 && cur->_bf == -1)//右的中间高
				{
					RotateRL(perent);//右左双旋
				}
				else
				{
					assert(false);
				}
				break;//旋转完直接结束
			}
			else
			{
				assert(false);//说明出现bug，报错处理
			}
		}

		return true;
	}

	//查找
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (key > cur->_kv.first)
			{
				cur = cur->_right;
			}
			else if (key < cur->_kv.first)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}

	//中序遍历
	void InOrder()
	{
		_InOrder(_root);
	}

	//判断是否为AVL树
	bool IsBalanceTree()
	{
		return _IsBalanceTree(_root);
	}

	//计算高度
	int Height()
	{
		return _Height(_root);
	}

	//计算大小
	int Size()
	{
		return _Size(_root);
	}

private:
	//右单旋
	void RotateR(Node* perent)
	{
		Node* subL = perent->_left;//左孩子
		Node* subLR = subL->_right;//左孩子的右孩子

		//旋转过去
		perent->_left = subLR;
		subL->_right = perent;

		Node* ppNode = perent->_perent;//记录父父节点

		//更新父节点指针
		if (subLR)
			subLR->_perent = perent;
		perent->_perent = subL;

		if (perent == _root)//判断根节点
		{
			_root = subL;
			subL->_perent = nullptr;
		}
		else
		{
			if (ppNode->_left == perent)
			{
				ppNode->_left = subL;
			}
			else
			{
				ppNode->_right = subL;
			}
			subL->_perent = ppNode;
		}

		//更新平衡因子
		subL->_bf = perent->_bf = 0;
	}

	//左单旋
	void RotateL(Node* perent)
	{
		Node* subR = perent->_right;
		Node* subRL = subR->_left;

		//旋转过去
		perent->_right = subRL;
		subR->_left = perent;

		Node* ppNode = perent->_perent;//父父节点

		//更新父节点指针
		if (subRL)
			subRL->_perent = perent;
		perent->_perent = subR;

		//判断父父节点
		if (perent == _root)
		{
			_root = subR;
			subR->_perent = nullptr;
		}
		else
		{
			if (ppNode->_left == perent)
			{
				ppNode->_left = subR;
			}
			else
			{
				ppNode->_right = subR;
			}
			subR->_perent = ppNode;
		}

		subR->_bf = perent->_bf = 0;
	}

	//左右双旋
	void RotateLR(Node* perent)
	{
		Node* subL = perent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;//单旋前先记录平衡因子

		RotateL(perent->_left);//先对左子树进行左单旋
		RotateR(perent);//对整体进行右单旋

		if (bf == -1)//左边高
		{
			perent->_bf = 1;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
		else if (bf == 1)//右边高
		{
			perent->_bf = 0;
			subL->_bf = -1;
			subLR = 0;
		}
		else if (bf == 0)//自己就是新增节点
		{
			perent->_bf = 0;
			subL->_bf = 0;
			subLR = 0;
		}
		else//如果真走到这里说明有bug
		{
			assert(false);
		}
	}

	//右左双旋
	void RotateRL(Node* perent)
	{
		Node* subR = perent->_right;
		Node* subRL = subR->_left;
		int bf = subRL->_bf;//先记录平衡因子

		RotateR(perent->_right);//先右单旋
		RotateL(perent);//再左单旋

		if (bf == -1)
		{
			perent->_bf = 0;
			subR->_bf = 1;
			subRL->_bf = 0;
		}
		else if (bf == 1)
		{
			perent->_bf = -1;
			subR->_bf = 0;
			subRL->_bf = 0;
		}
		else if (bf == 0)
		{
			perent->_bf = 0;
			subR->_bf = 0;
			subRL->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}

	void _InOrder(Node* root)//中序遍历
	{
		if (root == nullptr)
			return;
		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		_InOrder(root->_right);
	}

	int _Size(Node* root)
	{
		if (root == nullptr)
			return 0;
		int left = _Size(root->_left);
		int right = _Size(root->_right);

		return left + right + 1;
	}

	int _Height(Node* root)//计算高度
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}

	bool _IsBalanceTree(Node* root)//判断是否为AVL树
	{
		//空树也是AVL树
		if (nullptr == root)
			return true;

		//计算pRoot结点的平衡因子：即pRoot左右子树的高度差
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		int diff = rightHeight - leftHeight;

		// 如果计算出的平衡因子与pRoot的平衡因子不相等，或者
		// pRoot平衡因子的绝对值超过1，则⼀定不是AVL树
		if (abs(diff) >= 2)
		{
			cout << root->_kv.first << "高度差异常" << endl;
			return false;
		}
		if (root->_bf != diff)
		{
			cout << root->_kv.first << "平衡因子异常" << endl;
			return false;
		}
		//pRoot的左和右如果都是AVL树，则该树⼀定是AVL树
		return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
	}

	Node* _root = nullptr;
};
